package me.zhengjie.config;


/**
 * @author krilo
 * @date 2024-09-09 14:58
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableScheduling
public class SchedulerConfig {

    /**
     * 给定时任务设置线程池，避免多个定时任务用一个线程，导致定时任务执行时间过长的情况
     * @return
     */
    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(8); // 设置线程池大小
        scheduler.setThreadNamePrefix("ScheduledTask-"); // 线程名称前缀
        scheduler.setWaitForTasksToCompleteOnShutdown(true); // 应用关闭时等待任务完成
        scheduler.setAwaitTerminationSeconds(60); // 等待任务完成的超时时间
        return scheduler;
    }
}